const Router = require('@koa/router');
const { checkAuth, checkPermission } = require('../middlewares/auth');
const campusAccountController = require('../controllers/campusAccount.controller');

const router = new Router({ prefix: '/api/campus-accounts' });

/**
 * @swagger
 * /api/campus-accounts:
 *   post:
 *     tags: [校园账户]
 *     summary: 创建校园账户
 *     description: 为用户创建新的校园账户（学生或教师）
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - userId
 *               - accountType
 *             properties:
 *               userId:
 *                 type: string
 *                 description: 用户ID
 *                 example: "user_123"
 *               accountType:
 *                 type: string
 *                 enum: [teacher, student]
 *                 description: 账户类型（教师/学生）
 *                 example: "student"
 *               employeeId:
 *                 type: string
 *                 description: 教职工号（教师必填）
 *                 example: "T2024001"
 *               department:
 *                 type: string
 *                 description: 所属院系
 *                 example: "计算机科学与技术学院"
 *               grade:
 *                 type: string
 *                 description: 年级（学生必填）
 *                 example: "2024"
 *               class:
 *                 type: string
 *                 description: 班级（学生必填）
 *                 example: "计科2401"
 *               major:
 *                 type: string
 *                 description: 专业（学生必填）
 *                 example: "计算机科学与技术"
 *               admissionYear:
 *                 type: integer
 *                 description: 入学年份（学生必填）
 *                 example: 2024
 *               graduationYear:
 *                 type: integer
 *                 description: 预计毕业年份（学生必填）
 *                 example: 2028
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "ca_123"
 *                 message:
 *                   type: string
 *                   example: "校园账户创建成功"
 *       400:
 *         description: 参数错误
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 error:
 *                   type: string
 *                   example: "账户类型必填"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.post('/', checkAuth, checkPermission(['campus_account:create']), campusAccountController.createCampusAccount);

/**
 * @swagger
 * /api/campus-accounts:
 *   get:
 *     tags: [校园账户]
 *     summary: 获取校园账户列表
 *     description: 获取所有校园账户的列表（支持分页和基本筛选）
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: accountType
 *         schema:
 *           type: string
 *           enum: [teacher, student]
 *         description: 账户类型筛选
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 total:
 *                   type: integer
 *                   example: 100
 *                 pages:
 *                   type: integer
 *                   example: 10
 *                 current:
 *                   type: integer
 *                   example: 1
 *                 accounts:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                         example: "ca_123"
 *                       userId:
 *                         type: string
 *                         example: "user_123"
 *                       accountType:
 *                         type: string
 *                         example: "student"
 *                       department:
 *                         type: string
 *                         example: "计算机科学与技术学院"
 *                       status:
 *                         type: string
 *                         example: "active"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/', checkAuth, checkPermission(['campus_account:read']), campusAccountController.getCampusAccounts);

/**
 * @swagger
 * /api/campus-accounts/search:
 *   get:
 *     tags: [校园账户]
 *     summary: 搜索校园账户
 *     description: 根据多个条件搜索校园账户
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: accountType
 *         schema:
 *           type: string
 *           enum: [teacher, student]
 *         description: 账户类型
 *       - in: query
 *         name: department
 *         schema:
 *           type: string
 *         description: 院系
 *       - in: query
 *         name: grade
 *         schema:
 *           type: string
 *         description: 年级
 *       - in: query
 *         name: major
 *         schema:
 *           type: string
 *         description: 专业
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [active, inactive, graduated]
 *         description: 账户状态
 *     responses:
 *       200:
 *         description: 搜索成功
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 type: object
 *                 properties:
 *                   id:
 *                     type: string
 *                     example: "ca_123"
 *                   accountType:
 *                     type: string
 *                     example: "student"
 *                   department:
 *                     type: string
 *                     example: "计算机科学与技术学院"
 *                   grade:
 *                     type: string
 *                     example: "2024"
 *                   major:
 *                     type: string
 *                     example: "计算机科学与技术"
 *                   status:
 *                     type: string
 *                     example: "active"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/search', checkAuth, checkPermission(['campus_account:read']), campusAccountController.searchCampusAccounts);

/**
 * @swagger
 * /api/campus-accounts/{id}:
 *   get:
 *     tags: [校园账户]
 *     summary: 获取校园账户详情
 *     description: 获取指定校园账户的详细信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 校园账户ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "ca_123"
 *                 userId:
 *                   type: string
 *                   example: "user_123"
 *                 accountType:
 *                   type: string
 *                   example: "student"
 *                 employeeId:
 *                   type: string
 *                   example: "T2024001"
 *                 department:
 *                   type: string
 *                   example: "计算机科学与技术学院"
 *                 grade:
 *                   type: string
 *                   example: "2024"
 *                 class:
 *                   type: string
 *                   example: "计科2401"
 *                 major:
 *                   type: string
 *                   example: "计算机科学与技术"
 *                 status:
 *                   type: string
 *                   example: "active"
 *                 academicStatus:
 *                   type: string
 *                   example: "normal"
 *                 admissionYear:
 *                   type: integer
 *                   example: 2024
 *                 graduationYear:
 *                   type: integer
 *                   example: 2028
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 账户不存在
 */
router.get('/:id', checkAuth, checkPermission(['campus_account:read']), campusAccountController.getCampusAccount);

/**
 * @swagger
 * /api/campus-accounts/{id}:
 *   put:
 *     tags: [校园账户]
 *     summary: 更新校园账户
 *     description: 更新指定校园账户的信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 校园账户ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               employeeId:
 *                 type: string
 *                 description: 教职工号
 *                 example: "T2024001"
 *               department:
 *                 type: string
 *                 description: 院系
 *                 example: "计算机科学与技术学院"
 *               grade:
 *                 type: string
 *                 description: 年级
 *                 example: "2024"
 *               class:
 *                 type: string
 *                 description: 班级
 *                 example: "计科2401"
 *               major:
 *                 type: string
 *                 description: 专业
 *                 example: "计算机科学与技术"
 *               admissionYear:
 *                 type: integer
 *                 description: 入学年份
 *                 example: 2024
 *               graduationYear:
 *                 type: integer
 *                 description: 预计毕业年份
 *                 example: 2028
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "校园账户更新成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 账户不存在
 */
router.put('/:id', checkAuth, checkPermission(['campus_account:update']), campusAccountController.updateCampusAccount);

/**
 * @swagger
 * /api/campus-accounts/{id}:
 *   delete:
 *     tags: [校园账户]
 *     summary: 删除校园账户
 *     description: 删除指定的校园账户
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 校园账户ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "校园账户删除成功"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 账户不存在
 */
router.delete('/:id', checkAuth, checkPermission(['campus_account:delete']), campusAccountController.deleteCampusAccount);

/**
 * @swagger
 * /api/campus-accounts/{id}/status:
 *   patch:
 *     tags: [校园账户]
 *     summary: 更新账户状态
 *     description: 更新校园账户的状态（在读/休学/毕业等）
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 校园账户ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - status
 *             properties:
 *               status:
 *                 type: string
 *                 enum: [active, inactive, graduated, suspended]
 *                 description: 账户状态
 *                 example: "active"
 *     responses:
 *       200:
 *         description: 状态更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "账户状态更新成功"
 *       400:
 *         description: 状态值无效
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 账户不存在
 */
router.patch('/:id/status', checkAuth, checkPermission(['campus_account:update']), campusAccountController.updateStatus);

/**
 * @swagger
 * /api/campus-accounts/{id}/academic-status:
 *   patch:
 *     tags: [校园账户]
 *     summary: 更新学业状态
 *     description: 更新学生的学业状态（正常/警告/暂停）
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 校园账户ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - academicStatus
 *             properties:
 *               academicStatus:
 *                 type: string
 *                 enum: [normal, probation, suspended]
 *                 description: 学业状态
 *                 example: "normal"
 *     responses:
 *       200:
 *         description: 学业状态更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "学业状态更新成功"
 *       400:
 *         description: 状态值无效
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 账户不存在
 */
router.patch('/:id/academic-status', checkAuth, checkPermission(['campus_account:update']), campusAccountController.updateAcademicStatus);

module.exports = router; 